package org.wisdom.data.jpa.config;

import java.util.Optional;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.wisdom.context.WisdomContextHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * JPA配置类
 *
 * @author zhangzhiyan
 * @date 2021/07/06 17:08
 **/
@Configuration
@EnableJpaAuditing
@Slf4j
@ComponentScan(value = {"org.wisdom.data.jpa.support","org.wisdom.data.jpa.filter"})
@EntityScan("org.wisdom.data.jpa.support")
@EnableJpaRepositories("org.wisdom.data.jpa.support")
@PropertySource(value = {"classpath:wisdom_datasource.properties"}, encoding = "utf-8")
public class JpaConfiguration implements AuditorAware<String> {

    @SuppressWarnings("NullableProblems")
    @Override
    public Optional<String> getCurrentAuditor() {
        String userId = WisdomContextHolder.getContext().getUserId();
        return Optional.of(userId);
    }
}
